package com.zizy.healthexpert.Database;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

import com.zizy.healthexpert.Config.Config;

import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by Zizy on 2014/8/5.
 */
public class MyDatabase_PersonalBasicInf extends MyDataBase {
    public static final String TABLE_NAME = "PersonalBasicInf";
    public MyDatabase_PersonalBasicInf(Context context) {
        super(context);
        initMyDatabase();
    }

    @Override
    public void initMyDatabase() {
        CreateTable();
    }

    @Override
    public ArrayList<Object> SearchDatabase(String selectWhat, String where_expression) {
        ArrayList<Object> list = new ArrayList<Object>();
        FieldsBasicInf fieldsBasicInf= null;

        Cursor cursor = db.rawQuery(String.format(
                "select %s from %s where %s;", selectWhat, TABLE_NAME,
                where_expression), null);
        while (cursor.moveToNext()) {
            fieldsBasicInf = new FieldsBasicInf( cursor.getInt(0),
                     cursor.getInt(1),cursor.getInt(2),cursor.getInt(3) );
            list .add(fieldsBasicInf);
        }
        cursor.close();
        return list;
    }

    @Override
    public ArrayList<Object> SearchDatabase(String selectWhat) {
        ArrayList<Object> list = new ArrayList<Object>();
        FieldsBasicInf fieldsBasicInf= null;

        Cursor cursor = db.rawQuery(String.format(
                "select %s from %s ;", selectWhat, TABLE_NAME
                ), null);
        while (cursor.moveToNext()) {
            fieldsBasicInf = new FieldsBasicInf( cursor.getInt(0),
                    cursor.getInt(1),cursor.getInt(2),cursor.getInt(3) );
            list .add(fieldsBasicInf);
        }
        cursor.close();
        return list;
    }

    @Override
    public void insertDatabase(Object... objects) {
        int id= Config.userID;
        int age= (Integer)objects[0];
        int gender = (Integer) objects[1];
        int labour = (Integer) objects[2];
        this.db.execSQL(String
                .format("insert into %s (ID,Age,Gender,Labour) values('%s','%s','%s','%s' )",
                        TABLE_NAME, id, age,gender,labour));
    }

    @Override
    public void updateDatabase(Object... objects) {
        int id= Config.userID;
        int age= (Integer)objects[0];
        int gender = (Integer) objects[1];
        int labour = (Integer) objects[2];
        this.db.execSQL(String
                .format("update %s set Age='%s',Gender='%s', Labour='%s'  where ID='%s'",
                        TABLE_NAME,  age,gender,labour,id));
    }

    @Override
    public void CreateTable() {
        try {
            String sql = "create table if not exists " + TABLE_NAME
                    + " (ID INTEGER, "
                    + "Age INTEGER ," + "Gender INTEGER, "
                    + "Labour INTEGER);";
            db.execSQL(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public class FieldsBasicInf{
        int id;
        int age;
        int gender;
        int labour;

        public int getId() {
            return id;
        }

        public int getAge() {
            return age;
        }

        public int getGender() {
            return gender;
        }

        public int getLabour() {
            return labour;
        }

        public FieldsBasicInf(int id, int age, int gender, int labour) {
            this.id = id;
            this.age = age;
            this.gender = gender;
            this.labour = labour;
        }
    }
}
